package StudyCode.Collections;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * @author lixiaoyao
 * @date 2018/11/8 0008、上午 8:25
 * @description 链表测试
 */
public class LinkedListTest {

	public static void main(String[] args) {
		List<String> a = new LinkedList<>();
		a.add("Amy");
		a.add("Carl");
		a.add("Erica");

		List<String> b = new LinkedList<>();
		b.add("Bob");
		b.add("Doug");
		b.add("Frances");
		b.add("Gloria");

		// merge the words from b into a
		ListIterator<String> aIterator = a.listIterator();
		Iterator<String> bIterator = b.iterator();

		while (bIterator.hasNext()) {
			if (aIterator.hasNext()) {
				aIterator.next();
				aIterator.add(bIterator.next());
			} else {
				break;
			}
		}
		System.out.println(a);
		// remove every second word form b

		bIterator = b.iterator();
		while (bIterator.hasNext()) {
			bIterator.next(); // skip one element
			if (bIterator.hasNext()) {
				bIterator.next(); // skip next element
				bIterator.remove(); // remove that element
			}
		}

		System.out.println(b);

		// bulk operation: remove all words in b form a

		a.removeAll(b);

		System.out.println(a);
	}

}
